package com.sunny.ynhz.utils;

import android.content.Context;
import android.util.Log;

import com.sunny.ynhz.api.HttpManager;
import com.sunny.ynhz.api.subscriber.ApiSubscriber;
import com.sunny.ynhz.ui.base.CommentBaseWebActivity;

import org.json.JSONException;
import org.json.JSONObject;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by huhuan123 on 2018/1/8.
 */

public class CommentUtils {
    public static void updateComment(String problemId, String userId, String type, final String content, final Context context) {
        HttpManager.getInstance().updateComment(problemId, userId, type, content).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new ApiSubscriber(context, new ApiSubscriber.HttpOnResultListener<JSONObject>() {
            @Override
            public void onNext(JSONObject jsonObject) {
                CommentBaseWebActivity baseWebActivity = (CommentBaseWebActivity) context;
                baseWebActivity.upCommentSuccess();
                
                getStarNum(problemId,context);
            }
            @Override
            public void onCompleted() {
                ShowUtils.closePopwindow();
            }

            @Override
            public void onstart() {
                ShowUtils.inItPopwin(context, "请稍后");

            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                Log.e("hu", e.toString());
                ShowUtils.closePopwindow();
            }
        }));
    }

    public static void getStarNum(String newsId, final Context context) {
        HttpManager.getInstance().getStarNum(newsId).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new ApiSubscriber(context, new ApiSubscriber.HttpOnResultListener<JSONObject>() {
            @Override
            public void onNext(JSONObject jsonObject) {
                Log.e("hu", jsonObject.toString());
                CommentBaseWebActivity baseWebActivity = (CommentBaseWebActivity) context;
                try {
                    String startNum = jsonObject.getJSONObject("data").get("thumbsNums").toString();
                    String commentNums = jsonObject.getJSONObject("data").get("commentNums").toString();
                    String readNums = jsonObject.getJSONObject("data").get("readNums").toString();
                    int start = startNum.equals("") ? 0 : Integer.parseInt(startNum);
                    int comment = commentNums.equals("") ? 0 : Integer.parseInt(commentNums);
                    int readNum = readNums.equals("") ? 0 : Integer.parseInt(readNums);
                    baseWebActivity.getStart(start, readNum, comment);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onCompleted() {
            }

            @Override
            public void onstart() {

            }
        }));
    }

    public static void toStar(String proId, final Context context) {
        HttpManager.getInstance().toStar(proId, SPUtil.get("userId", "").toString(), "1").observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(new ApiSubscriber(context, new ApiSubscriber.HttpOnResultListener<JSONObject>() {
            @Override
            public void onNext(JSONObject o) {
                Log.e("hu", o.toString());
                CommentBaseWebActivity baseWebActivity = (CommentBaseWebActivity) context;
                try {
                    baseWebActivity.starResult(o.get("data").toString().equals("") ? 0 : Integer.parseInt(o.get("data").toString()));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onCompleted() {


            }

            @Override
            public void onstart() {

            }
        }));

    }
}
